package com.shxy.rlzy.controller;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shxy.rlzy.common.lang.Result;
import com.shxy.rlzy.controller.salary.NewTask;
import com.shxy.rlzy.entity.SalaryTask;
import com.shxy.rlzy.entity.SalaryTaskDepart;
import com.shxy.rlzy.service.ISalaryTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tanhao
 * @since 2022-03-19
 */
@RestController
@RequestMapping("/salary_task")
public class SalaryTaskController extends BaseController {
    @Autowired
    private ISalaryTaskService salaryTaskService;


    /**
     * 查询任务列表
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/list")
    public Result listAll(@RequestParam(defaultValue = "1") int current, @RequestParam(defaultValue = "10") int size) {
        // 1、分页查询任务列表
        Page<SalaryTask> pageList = salaryTaskService.page(new Page<>(current, size));
        // 2、构建结果
        return Result.succ(pageList);
    }

    /**
     * 查询指定状态的任务列表
     * @param current
     * @param size
     * @param statu
     * @return
     */
    @GetMapping("/list/{statu}")
    public Result listStatu(@RequestParam(defaultValue = "1") int current,
                            @RequestParam(defaultValue = "10") int size,
                            @PathVariable int statu) {
        Page<SalaryTask> pageList = salaryTaskService.pageOfStatu(new Page<>(current, size), statu);
        return Result.succ(pageList);
    }

    /**
     * 查询任务列表
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/list/{current}/{size}")
    public Result pagedList(@PathVariable int current,
                            @PathVariable int size) {
        // 获取总数
        long total = salaryTaskService.count();
        // 分页查询列表
        List<SalaryTask> records = salaryTaskService.getPagedList(current, size);
        JSONObject data = new JSONObject();
        data.set("total", total);
        data.set("size", size);
        data.set("current", current);
        data.set("records", records);

        return Result.succ(data);
    }

    /**
     * 创建任务
     * @param task
     * @return
     */
    @PutMapping("/create")
    @PreAuthorize("hasAnyAuthority('wage:task:create')")
    public Result addTask(@RequestBody NewTask task) {
        // 1、创建新任务
        SalaryTask st = new SalaryTask();
        st.setName(task.getName());
        st.setEndTime(task.getEndTime());
        // 2、创建部门任务列表
        List<SalaryTaskDepart> stds = task.getDeparts().stream().map(taskDepart -> {
            SalaryTaskDepart std = new SalaryTaskDepart();
            std.setDepartId(taskDepart.getId());
            std.setBonus(taskDepart.getBonus());
            return std;
        }).collect(Collectors.toList());
        // 3、保存任务
        SalaryTask salaryTask = salaryTaskService.addNew(st, stds);
        return Result.succ(salaryTask);
    }

    /**
     * 删除任务
     * @param taskId
     * @return
     */
    @DeleteMapping("/delete/{taskId}")
    @PreAuthorize("hasAnyAuthority('wage:task:delete')")
    public Result deleteTask(@PathVariable long taskId) {
        salaryTaskService.deleteTaskById(taskId);
        return Result.succ();
    }

    @PutMapping("/change_statu/{taskId}/{statu}")
    @PreAuthorize("hasAnyAuthority('wage:task:statu')")
    public Result changeStatu(@PathVariable long taskId, @PathVariable int statu) {
        salaryTaskService.changeStatuById(taskId, statu);
        return Result.succ();
    }
}
